package pager;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tikeyc.mobileplayer.R;

import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.AudioNetPagerAdapter;
import adapter.VideoPagerAdapter;
import base.BasePager;
import common.Constants;
import domain.MediaItem;
import domain.NetAudioPageItem;
import utils.CacheUtils;
import utils.StringUtils;
import view.DropDownListView;

import static com.tikeyc.mobileplayer.R.id.listView;
import static com.tikeyc.mobileplayer.R.id.textView;

/**
 * Created by public1 on 2017/1/4.
 */

/**
 * http://s.budejie.com/topic/list/jingxuan/1/budejie-android-6.2.8/0-20.json?market=baidu&udid=86342502659959&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=98%3Ac%3Af5%3A4b%3A72%3A6d&ver=6.2.2
 *jcvideoplayer
 * */

public class AudioNetPager extends BasePager {


    private static final String CACHE_KEY = "audio_net_pager_cache_key";

    private DropDownListView listView;
    private TextView textView_no_media;
    private ProgressBar progressBar;



    private List<NetAudioPageItem.ListBean> audioItems;

    private AudioNetPagerAdapter listAdapter;
    /**
     * 刷新UI
     */
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (audioItems != null && audioItems.size() > 0) {
                isInitData = true;
                textView_no_media.setVisibility(View.GONE);

                //
                listAdapter = new AudioNetPagerAdapter(context,audioItems);
                listView.setAdapter(listAdapter);
            } else {
                isInitData = false;
                textView_no_media.setVisibility(View.VISIBLE);
            }

            //
            progressBar.setVisibility(View.GONE);
        }
    };


    public AudioNetPager(Context context) {
        super(context);

    }

    @Override
    public View initView() {

        View view = View.inflate(context, R.layout.audio_net_pager,null);

        listView = (DropDownListView) view.findViewById(R.id.listView);

        textView_no_media = (TextView) view.findViewById(R.id.textView_no_media);
        textView_no_media.setText("没有发现音频");
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);


        listView.setDropDownStyle(true);
        listView.setOnBottomStyle(true);
        listView.setOnDropDownListener(new DropDownListView.OnDropDownListener() {
            @Override
            public void onDropDown() {
                Log.e("TAG","setOnDropDownListener");
                getDataFromNet();
            }
        });

        listView.setOnBottomListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("TAG","setOnBottomListener");
                Toast.makeText(context,"无更多",Toast.LENGTH_SHORT).show();
                listView.setOnBottomStyle(false);
            }
        });

        return view;
    }


    @Override
    public void initData() {
        super.initData();

        loadData();


        Log.e("TAG","网络音乐界面");
    }


    private void loadData() {
        String cacheString = CacheUtils.getCacheString(context,CACHE_KEY);
        if (StringUtils.isEmpty(cacheString)){
            listView.onDropDownComplete();
            listView.onBottomComplete();
            try {
                //先显示缓存
                paramsData(cacheString);
                //在加载最新数据
                getDataFromNet();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            getDataFromNet();
        }
    }

    private void getDataFromNet() {
        final RequestParams params = new RequestParams(Constants.AUDIO_NET_URL);
        x.http().get(params, new Callback.CommonCallback<String >() {

            @Override
            public void onSuccess(String result) {
                LogUtil.e("onSuccess"+result);
                Log.e("TAG",result);
                //
                CacheUtils.cacheString(context,CACHE_KEY,result);
                try {
                    paramsData(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("onError"+ex.getMessage());
                //刷新UI
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled"+cex.getMessage());
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished");
                listView.onDropDownComplete();
                listView.onBottomComplete();
            }
        });
    }


    /**数据解析 1.用GsonFormat 生成bean对象 2.用gson
     * @param result
     */
    private void paramsData(String result) throws JSONException {

        NetAudioPageItem netAudioPageItem = new Gson().fromJson(result, NetAudioPageItem.class);

        audioItems = netAudioPageItem.getList();
        //刷新UI
        handler.sendEmptyMessage(0);

//        isInitData = true;

    }

}
